959. Regions Cut By Slashes
959. Regions Cut By Slashes
Description
Solution
Two solutions both work:
- DFS, like find connected components in a
island-sea question
, but we need to deal with the slashes because we can not DFS a half-block. So we can zoom up the grid into 3 * 3 array then map the slash into it. - Use union-find to union the plots and edges.
- First, each plot are in its own set, four edges are in one set
- We note that once slash connect two points from the same set, we can get 1 new region. Once points are from different set, we just union them.
Code
1 | class Solution { |
1 | class Solution: |
All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.